import { Controller, Get, Inject } from '@nestjs/common';
import { SystemService } from '../../services/admin/system.service';
import type { ILanguageService } from '@wwjCommon/language/language.interface';

/**
 * 系统信息控制器
 * 基于PHP的System控制器实现
 * 
 * 路由前缀: /adminapi/sys/system
 * 功能: 系统信息查询、环境检测、缓存管理
 */
@Controller('adminapi/sys/system')
export class SystemController {
  constructor(
    private readonly systemService: SystemService,
    @Inject('ILanguageService') private readonly languageService: ILanguageService,
  ) {}

  /**
   * 获取当前系统信息
   * 对应PHP: System::info()
   */
  @Get('info')
  async getInfo() {
    try {
      const result = await this.systemService.getInfo();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('data_get_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 获取当前URL配置
   * 对应PHP: System::url()
   */
  @Get('url')
  async getUrl() {
    try {
      const result = await this.systemService.getUrl();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('data_get_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 获取系统环境配置
   * 对应PHP: System::getSystemInfo()
   */
  @Get('system-info')
  async getSystemInfo() {
    try {
      const result = await this.systemService.getSystemInfo();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('data_get_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 清理表缓存
   * 对应PHP: System::schemaCache()
   */
  @Get('schema-cache')
  async schemaCache() {
    try {
      const result = await this.systemService.schemaCache();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('cache_clear_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 清理缓存
   * 对应PHP: System::clearCache()
   */
  @Get('clear-cache')
  async clearCache() {
    try {
      const result = await this.systemService.clearCache();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('cache_clear_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 校验消息队列是否正常运行
   * 对应PHP: System::checkJob()
   */
  @Get('check-job')
  async checkJob() {
    try {
      const result = await this.systemService.checkJob();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('job_check_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 校验计划任务是否正常运行
   * 对应PHP: System::checkSchedule()
   */
  @Get('check-schedule')
  async checkSchedule() {
    try {
      const result = await this.systemService.checkSchedule();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('schedule_check_fail'),
        error: error.message,
      };
    }
  }

  /**
   * 环境变量查询
   * 对应PHP: System::getEnvInfo()
   */
  @Get('env-info')
  async getEnvInfo() {
    try {
      const result = await this.systemService.getEnvInfo();
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      return {
        code: 500,
        message: await this.languageService.getApiMessage('data_get_fail'),
        error: error.message,
      };
    }
  }
}